#include "mainwindow.h"
#include "experimentmethodui.h"
#include "ui_mainwindow.h"

#include <QStackedLayout>
#include <QMessageBox>

#include "mainui.h"
#include "experimentpropertyui.h"
#include "experimentplateui.h"
#include "experimentrunningui.h"
#include "experimentresultui.h"
#include "experimentexportui.h"
#include "experimentsettingui.h"
#include "experimentopenfileui.h"
#include "runningtemperatureui.h"
#include "errormessageui.h"
#include "maintenanceui.h"
#include "settingexportui.h"
#include "settingdefaultui.h"
#include "settingdatastatisticui.h"
#include "settingconnectionui.h"
#include "settingrestoreui.h"
#include "settingpreferenceui.h"
#include "settingmodelconfigui.h"
#include "openfileothersfileanalysisui.h"

#include "messagedialog.h"
#include "messageImgdialog.h"
#include "passwordui.h"
#include "GLOBAL.h"

#include "qanimationstackedwidget.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    this->setWindowTitle("card-type-chip Analysis software V1.0.1");
    this->initWidget();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::resizeEvent(QResizeEvent *event)
{
    return QMainWindow::resizeEvent(event);
}

/**
 * @brief initWidget 初始化函数
 */
void MainWindow::initWidget(){
    // 容器
    this->container = new QWidget();
    this->containerLayout = new QStackedLayout();

    this->mainPageWidget = new QStackedWidget(); // 创建主实验窗口容器
    // this->mainPageWidget = new QAnimationStackedWidget(); // 创建主实验窗口容器
    // this->mainPageWidget->setDuration(300);

    // 主页窗口
    MainUI *mainUi = new MainUI();
    mainUi->move(0, 0);
    quickWidgetList.append(mainUi); // 加入数组
    this->mainPageWidget->addWidget(mainUi); // 加入窗口
    // 绑定信号槽
    connect(mainUi, &MainUI::prevPage, this, &MainWindow::onPrevPage);
    connect(mainUi, &MainUI::nextPage, this, &MainWindow::onNextPage);

    // 实验属性
    ExperimentPropertyUI *expProperty = new ExperimentPropertyUI();
    expProperty->move(0, 0);
    quickWidgetList.append(expProperty); // 加入数组
    this->mainPageWidget->addWidget(expProperty); // 加入窗口
    // 绑定信号槽
    connect(expProperty, &ExperimentPropertyUI::prevPage, this, &MainWindow::onPrevPage);
    connect(expProperty, &ExperimentPropertyUI::nextPage, this, &MainWindow::onNextPage);

    // 反应版
    ExperimentPlateUI *expPlate = new ExperimentPlateUI();
    expPlate->move(0, 0);
    quickWidgetList.append(expPlate);
    this->mainPageWidget->addWidget(expPlate);
    // 绑定信号槽
    connect(expPlate, &ExperimentPlateUI::prevPage, this, &MainWindow::onPrevPage);
    connect(expPlate, &ExperimentPlateUI::nextPage, this, &MainWindow::onNextPage);

    // 实验运行
    ExperimentRunningUI *expRunning = new ExperimentRunningUI();
    expRunning->move(0, 0);
    quickWidgetList.append(expRunning); // 加入数组
    this->mainPageWidget->addWidget(expRunning); // 加入窗口
    // 绑定信号槽
    connect(expRunning, &ExperimentRunningUI::prevPage, this, &MainWindow::onPrevPage);
    connect(expRunning, &ExperimentRunningUI::nextPage, this, &MainWindow::onNextPage);

    // 实验结果
    ExperimentResultUI *expResult = new ExperimentResultUI();
    expResult->move(0, 0);
    quickWidgetList.append(expResult); // 加入数组
    this->mainPageWidget->addWidget(expResult); // 加入窗口
    // 绑定信号槽
    connect(expResult, &ExperimentResultUI::prevPage, this, &MainWindow::onPrevPage);
    connect(expResult, &ExperimentResultUI::nextPage, this, &MainWindow::onNextPage);

    // 数据导出
    // ExperimentExportUI *expExport = new ExperimentExportUI();
    // expExport->move(0, 0);
    // quickWidgetList.append(expExport); // 加入数组
    // this->mainPageWidget->addWidget(expExport); // 加入窗口
    // 绑定信号槽
    // connect(expProperty, &ExperimentExportUI::prevPage, this, &MainWindow::onPrevPage);
    // connect(expProperty, &ExperimentExportUI::nextPage, this, &MainWindow::onNextPage);


    this->mainPageWidget->setCurrentIndex(0);
    this->pageCurrentIndex = mainPageWidget->currentIndex();

    this->containerLayout->addWidget(mainPageWidget);

    RunningTemperatureUI *runningTemp = new RunningTemperatureUI();
    // this->setCentralWidget(runningTemp);

    ErrorMessageUI *errMsgUi = new ErrorMessageUI();
    // this->setCentralWidget(errMsgUi);

    OpenFileOthersFileAnalysisUI *openOthersFileAnalysis = new OpenFileOthersFileAnalysisUI();
    // this->setCentralWidget(openOthersFileAnalysis);

    ExperimentOpenFileUI *expOpenFile = new ExperimentOpenFileUI();
    connect(expOpenFile, &ExperimentOpenFileUI::returnSig, this, &MainWindow::onReturnSig);
    connect(expOpenFile, &ExperimentOpenFileUI::cancel, this, [=](){
        QWidget::update();
        this->update();
        // this->repaint();
    });
    this->containerLayout->addWidget(expOpenFile);

    MaintenanceUI *maintenance = new MaintenanceUI();
    connect(maintenance, &MaintenanceUI::returnSig, this, &MainWindow::onReturnSig);
    // 维护密码
    PasswordUI *maintenancePwdUi = new PasswordUI();
    maintenancePwdUi->setParent(this, Qt::Window | Qt::WindowCloseButtonHint);

    this->containerLayout->addWidget(maintenance);

    // 设置页面的stackedWidget
    this->settingStackedWidget = new QStackedWidget();

    ExperimentSettingUI *expSetting = new ExperimentSettingUI();
    connect(expSetting, &ExperimentSettingUI::returnSig, this, &MainWindow::onReturnSig);
    connect(expSetting, &ExperimentSettingUI::changeSettingPage, this, &MainWindow::onChangeSettingPage);
    this->settingStackedWidget->addWidget(expSetting);

    SettingExportUI *settingExport = new SettingExportUI();
    connect(settingExport, &SettingExportUI::settingReturnSig, this, &MainWindow::onSettingReturnSig);
    this->settingStackedWidget->addWidget(settingExport);
    // this->setCentralWidget(settingExport);

    SettingDefaultUI *settingDefault = new SettingDefaultUI();
    connect(settingDefault, &SettingDefaultUI::settingReturnSig, this, &MainWindow::onSettingReturnSig);
    this->settingStackedWidget->addWidget(settingDefault);
    // this->setCentralWidget(settingDefault);

    SettingDataStatisticUI *settingDataStas = new SettingDataStatisticUI();
    connect(settingDataStas, &SettingDataStatisticUI::settingReturnSig, this, &MainWindow::onSettingReturnSig);
    this->settingStackedWidget->addWidget(settingDataStas);
    // this->setCentralWidget(settingDataStas);

    SettingPreferenceUI *settingPreference = new SettingPreferenceUI();
    connect(settingPreference, &SettingPreferenceUI::settingReturnSig, this, &MainWindow::onSettingReturnSig);
    this->settingStackedWidget->addWidget(settingPreference);
    // this->setCentralWidget(settingPreference);

    SettingConnectionUI *settingConnection = new SettingConnectionUI();
    connect(settingConnection, &SettingConnectionUI::settingReturnSig, this, &MainWindow::onSettingReturnSig);
    this->settingStackedWidget->addWidget(settingConnection);
    // this->setCentralWidget(settingConnection);

    SettingRestoreUI *settingRestore = new SettingRestoreUI();
    connect(settingRestore, &SettingRestoreUI::settingReturnSig, this, &MainWindow::onSettingReturnSig);
    this->settingStackedWidget->addWidget(settingRestore);
    // this->setCentralWidget(settingRestore);

    SettingModelConfigUI *settingModelConfig = new SettingModelConfigUI();
    connect(settingModelConfig, &SettingModelConfigUI::settingReturnSig, this, &MainWindow::onSettingReturnSig);
    this->settingStackedWidget->addWidget(settingModelConfig);
    // this->setCentralWidget(settingModelConfig);

    this->settingStackedWidget->setCurrentIndex(0);

    this->containerLayout->addWidget(settingStackedWidget);


    // 打开 程序 维护 设置
    connect(mainUi, &MainUI::opening, this, [=](){
        // DeviceSelfChecking *dfc = new DeviceSelfChecking();
        // dfc->show();

        this->containerLayout->setCurrentIndex(1);
    });
    connect(mainUi, &MainUI::program, this, [=](){
        MessageDialog::messageBox(
            NULL, Global::BoxType::Tips, "程序提示", "这里是程序提示",
            Global::StandardButton::Submit | Global::StandardButton::Cancel);
    });
    connect(mainUi, &MainUI::maintenance, this, [=](){
        // MessageImgDialog::messageBox(
        //     NULL, "维护", "这里是带图片的维护",
        //     Global::StandardButton::Submit | Global::StandardButton::Cancel);

        maintenancePwdUi->show();
        this->containerLayout->setCurrentIndex(2);
    });
    connect(mainUi, &MainUI::setting, this, [=](){
        this->containerLayout->setCurrentIndex(3);
    });

    // 扩增
    ExperimentMethodUI *expMethod = new ExperimentMethodUI();
    this->setCentralWidget(expMethod);

    this->container->setLayout(containerLayout);
    // this->setCentralWidget(container);
}

/**
 * @brief 切换至下一页
 */
void MainWindow::onNextPage()
{
    this->mainPageWidget->setCurrentIndex(pageCurrentIndex =
                                          (++pageCurrentIndex > 4) ? 4 : pageCurrentIndex);
    qDebug() << "MainWindow 切换下一页" << pageCurrentIndex;
}

/**
 * 切换至上一页
 */
void MainWindow::onPrevPage()
{
    this->mainPageWidget->setCurrentIndex(pageCurrentIndex =
                                          (--pageCurrentIndex < 0) ? 0 : pageCurrentIndex);
    qDebug() << "MainWindow 切换上一页" << pageCurrentIndex;
}

void MainWindow::onReturnSig()
{
    qDebug() << "返回";
    this->containerLayout->setCurrentIndex(0);
}

void MainWindow::onChangeSettingPage(int index)
{
    this->settingStackedWidget->setCurrentIndex(index);
}

void MainWindow::onSettingReturnSig()
{
    this->settingStackedWidget->setCurrentIndex(0);
}
